﻿/*******************************************************************************
* 
*Author         :  VN
*Description    :  网络通信模块线程模型
*Other          :  none
*Modify Record  :  none
*
*******************************************************************************/

using System.Collections.Generic;
using System.Collections;
using System.Text;
using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;

public abstract class BasicThread
{
    private Thread m_Thread;
    private bool   m_TerminateFlag;
    private System.Object m_TerminateFlagMutex;

    public BasicThread()
    {
        m_Thread = new Thread(ThreadProc);
        m_TerminateFlag = false;
        m_TerminateFlagMutex = new System.Object();
    }

    public void Run()
    {
        m_Thread.Start(this);
    }

    protected static void ThreadProc(object obj)
    {
        BasicThread me = (BasicThread)obj;
        me.Main();
    }

    protected abstract void Main();

    public void WaitTermination()
    {
        m_Thread.Join();
    }

    public void SetTerminateFlag()
    {
        lock (m_TerminateFlagMutex)
        {
            m_TerminateFlag = true;
        }
    }

    protected bool IsTerminateFlagSet()
    {
        lock (m_TerminateFlagMutex)
        {
            return m_TerminateFlag;
        }
    }
}

abstract class NetThread : BasicThread
{
    public NetThread(NetworkStream stream)
        : base()
    {
        netStream = stream;
    }

    protected NetworkStream NetStream
    {
        get
        {
            return netStream;
        }
    }

    private NetworkStream netStream;
}

